#include "ViewerCameraSceneNode.h"
#include "../include/PowerLibraryEngine.h"

#define CAM_INC		(vPos)(0.005f * FrameTime)
#define DEGRESS		(vPos)(0.05f * FrameTime)

u8 DebugCameraKeysType1[6]={KEY_W,KEY_S,KEY_A,KEY_D,KEY_Q,KEY_X};
u8 DebugCameraKeysType2[6]={KEY_T,KEY_G,KEY_F,KEY_H,KEY_R,KEY_B};
//u8 DebugCameraKeysType3[6]={KEY_T,KEY_G,KEY_F,KEY_H,KEY_R,KEY_B};

CViewerCameraSceneNode::CViewerCameraSceneNode(vPos angle,vPos aspect,vector3dvpos lookat,
											   vPos znear,vPos zfar,vector3dvpos vup,ISceneNodeReceiveParams):
zNear(znear),zFar(zfar),LookAt(lookat),Angle(angle),VUp(vup),Aspect(aspect),
EnableDebugMovementType1(false),EnableDebugMovementType2(false),EnableDebugMovementType3(false),
I3DCameraSceneNode(ISceneNodeBasicParams)
{
	ProjectionMatrix.MakeIdentity();
}

CViewerCameraSceneNode::~CViewerCameraSceneNode()
{

}

void CViewerCameraSceneNode::Render(IDrawableFaceManager *dfm)
{
}

vector3dvpos RotateAround(vector3dvpos &VectorToRotate,vector3dvpos &PointAround,vector3dvpos Degress)
{
	vPos Dist = PointAround.GetDist(VectorToRotate);
	IMatrix3D<vPos> Temp1(MCF_IDENTITY);
	IMatrix3D<vPos> Temp2(MCF_IDENTITY);
	Temp1.SetRotationDegrees(Degress);
	Temp2.SetTranslation(VectorToRotate);
	Temp1 *= Temp2;
	vector3dvpos Trans = Temp1.GetTranslation();
	return Trans;
}

void CViewerCameraSceneNode::OnRender()
{
	u32 FrameTime = TimeManager->GetFrameTime();
	if (EnableDebugMovementType1)
	{
		if (InputManager->GetKeyState(DebugCameraKeysType1[0])->PressedDown)
		{
			SetPositionZ(GetPositionZ()+CAM_INC);
			SetLookAtZ(GetLookAtZ()+CAM_INC);
		}
		if (InputManager->GetKeyState(DebugCameraKeysType1[1])->PressedDown)
		{
			SetPositionZ(GetPositionZ()-CAM_INC);
			SetLookAtZ(GetLookAtZ()-CAM_INC);
		}
		if (InputManager->GetKeyState(DebugCameraKeysType1[2])->PressedDown)
		{
			SetPositionX(GetPositionX()+CAM_INC);
			SetLookAtX(GetLookAtX()+CAM_INC);
		}
		if (InputManager->GetKeyState(DebugCameraKeysType1[3])->PressedDown)
		{
			SetPositionX(GetPositionX()-CAM_INC);
			SetLookAtX(GetLookAtX()-CAM_INC);
		}
		if (InputManager->GetKeyState(DebugCameraKeysType1[4])->PressedDown)
		{
			SetPositionY(GetPositionY()+CAM_INC);
			SetLookAtY(GetLookAtY()+CAM_INC);
		}
		if (InputManager->GetKeyState(DebugCameraKeysType1[5])->PressedDown)
		{
			SetPositionY(GetPositionY()-CAM_INC);
			SetLookAtY(GetLookAtY()-CAM_INC);
		}
	}

    bool set = false;

	if (EnableDebugMovementType2)
	{
		if (InputManager->GetKeyState(DebugCameraKeysType2[0])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(DEGRESS,0.0,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[1])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos((vPos)-1.0*DEGRESS,0.0,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[2])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,DEGRESS,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[3])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,(vPos)-1.0*DEGRESS,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[4])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,0.0,DEGRESS)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[5])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,0.0,(vPos)-1.0*DEGRESS)));
			set = true;
		}

	}

	if (EnableDebugMovementType3)
	{
		if (InputManager->GetKeyState(DebugCameraKeysType2[0])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(DEGRESS,0.0,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[1])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos((vPos)-1.0*DEGRESS,0.0,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[2])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,DEGRESS,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[3])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,(vPos)-1.0*DEGRESS,0.0)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[4])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,0.0,DEGRESS)));
			set = true;
		}
		if (InputManager->GetKeyState(DebugCameraKeysType2[5])->PressedDown)
		{
			SetPosition(RotateAround(GetPosition(),GetLookAt(),vector3dvpos(0.0,0.0,(vPos)-1.0*DEGRESS)));
			set = true;
		}
	}

    if (set)
        pLogDebug << "CameraPos: " << GetPosition().GetStr() << endl;

}
